# LCV Twitter Experiments Analysis File
# This file analyzes both LCVexp1 and LCVexp2

rm(list=ls())

# Uncomment and set working diretory
# setwd("")
load("LCVexp1.rdata")
load("LCVexp2.rdata")
source("LCVsource.r")
library(stargazer)
library(AER)
library(ggplot2)
library(dplyr)

# Table 2
select(LCVexp1, treat, tweetbutton, signed, tweeted) %>%
group_by(treat, tweetbutton) %>%
  summarize(N = n(),
            Signed = sum(signed),
            signed_percent = Signed/N,
            Tweeted = sum(tweeted),
            Tweeted_percent = Tweeted/N)

# Table 3
select(LCVexp2, treat, signed, tweeted) %>%
group_by(treat) %>%
  summarize(N = n(),
            Signed = sum(signed),
            signed_percent = Signed/N,
            Tweeted = sum(tweeted),
            Tweeted_percent = Tweeted/N)

select(LCVexp2, tweetbutton, signed, tweeted) %>%
subset(signed==1) %>%
group_by(tweetbutton) %>%
  summarize(N = n(),
            Signed = sum(signed),
            signed_percent = Signed/N,
            Tweeted = sum(tweeted),
            Tweeted_percent = Tweeted/N)

### Experiment 1A ###

treat.signed.1 <- lm(signed ~ as.factor(treat), data=LCVexp1)  # treatments work
treat.signed.diff.1 <- lm(signed ~ as.factor(treat), data=subset(LCVexp1, treat !="pub")) # no differential effect

treat.tweeted.1 <- lm(tweeted ~ as.factor(treat), data=LCVexp1)  # treatments work
treat.tweeted.diff.1 <- lm(tweeted ~ as.factor(treat), data=subset(LCVexp1, treat !="pub")) # followers > organizers

treat.signed.dm.1 <- lm(signed ~ treat=="pub", data=LCVexp1) 
treat.tweeted.dm.1 <- lm(tweeted ~ treat=="pub", data=LCVexp1) 

treat.signed.cov.1 <- lm(signed ~ as.factor(treat) + 
                           account_type + centrality_centered + num_followers_centered + days_on_twitter_centered + days_missing, data=LCVexp1)  
treat.tweeted.cov.1 <- lm(tweeted ~ as.factor(treat) + 
                            account_type + centrality_centered + num_followers_centered + days_on_twitter_centered + days_missing, data=LCVexp1)


### Experiment 1B ###

# check for imbalance
button.signed.1 <- lm(signed ~  as.factor(tweetbutton), data=LCVexp1) # imbalance
buttonbytreat.signed.1 <- lm(signed ~  as.factor(tweetbutton) + as.factor(treat)  + as.factor(tweetbutton):as.factor(treat),data=LCVexp1) # ?

# buttons
button.tweeted.1 <- lm(tweeted ~ as.factor(tweetbutton), data=subset(LCVexp1, signed==1))
buttonbytreat.tweeted.1 <- lm(tweeted ~ as.factor(tweetbutton) + as.factor(treat) + as.factor(tweetbutton):as.factor(treat), data=subset(LCVexp1, signed==1))

# Effect of following someone exposed to the button
exposure.1 <- lm(actual_exposure ~ exposure, weights=weights, data=subset(LCVexp1, probexposure > 0 & probexposure < 1))
exposure.signed.1 <- lm(signed ~ exposure, weights=weights, data=subset(LCVexp1, probexposure > 0 & probexposure < 1))
exposure.tweeted.1 <- lm(tweeted ~ exposure, weights=weights, data=subset(LCVexp1, probexposure > 0 & probexposure < 1))

exposure.signed.iv.1 <- ivreg(signed ~ actual_exposure | exposure, weights=weights, data=subset(LCVexp1, probexposure > 0 & probexposure < 1))
exposure.tweeted.iv.1 <- ivreg(tweeted ~ actual_exposure | exposure, weights=weights, data=subset(LCVexp1, probexposure > 0 & probexposure < 1))

### Experiment 2A ###

treat.signed.2 <- lm(signed ~ as.factor(treat), data=LCVexp2)  # treatments work
treat.signed.diff.2 <- lm(signed ~ as.factor(treat), data=subset(LCVexp2, treat !="pub")) # no differential effect

treat.tweeted.2 <- lm(tweeted ~ as.factor(treat), data=LCVexp2)  # treatments work
treat.tweeted.diff.2 <- lm(tweeted ~ as.factor(treat), data=subset(LCVexp2, treat !="pub")) # followers !> organizers (DIFFERENT FROM EXP 1)

treat.signed.cov.2 <- lm(signed ~ as.factor(treat) + 
                           account_type + centrality_centered + num_followers_centered + days_on_twitter_centered + days_missing, data=LCVexp2)  
treat.tweeted.cov.2 <- lm(tweeted ~ as.factor(treat) + 
                            account_type + centrality_centered + num_followers_centered + days_on_twitter_centered+ days_missing, data=LCVexp2)

### Experiment 2B ###

# check for imbalance
button.signed.2 <- lm(signed ~  as.factor(tweetbutton), data=LCVexp2) # NO imbalance (DIFF FROM EXP 1)

# buttons
button.tweeted.2 <- lm(tweeted ~ as.factor(tweetbutton), data=subset(LCVexp2, signed==1))
buttonbytreat.tweeted.2 <- lm(tweeted ~ as.factor(tweetbutton) + as.factor(treat) + as.factor(tweetbutton):as.factor(treat), data=subset(LCVexp2, signed==1))

# Effect of following someone exposed to the button
exposure.2 <- lm(actual_exposure ~ exposure, weights=weights, data=subset(LCVexp2, probexposure > 0 & probexposure < 1))
exposure.signed.2 <- lm(signed ~ exposure, weights=weights, data=subset(LCVexp2, probexposure > 0 & probexposure < 1))
exposure.tweeted.2 <- lm(tweeted ~ exposure, weights=weights, data=subset(LCVexp2, probexposure > 0 & probexposure < 1))

exposure.signed.iv.2 <- ivreg(signed ~ actual_exposure | exposure, weights=weights, data=subset(LCVexp2, probexposure > 0 & probexposure < 1))
exposure.tweeted.iv.2 <- ivreg(tweeted ~ actual_exposure | exposure, weights=weights, data=subset(LCVexp2, probexposure > 0 & probexposure < 1))


#### Experiment 1 tables

#sink("exp1Amain.tex")
stargazer(treat.signed.1, treat.signed.cov.1, treat.tweeted.1, treat.tweeted.cov.1,
          coef=makecoefslist(list(treat.signed.1, treat.signed.cov.1, treat.tweeted.1, treat.tweeted.cov.1)),
          se=makerobustseslist(list(treat.signed.1, treat.signed.cov.1, treat.tweeted.1, treat.tweeted.cov.1)),
          style="apsr", column.sep.width = "0pt",  digits=3, digits.extra=0,
          omit.stat=c("adj.rsq", "f", "ser"),
          dep.var.labels=c("Signed", "Tweeted"),
          covariate.labels=c("Treatment: Follower", "Treatment: Organizer", 
                             "Account Type: Male", "Account Type: Organization", "Account Type: Unknown",
                             "Eigenvector Centrality", "Number of Followers", "Days on Twitter", "Days on Twitter Missing",  "Constant"),
          align=TRUE,
          notes=c("Robust standard errors in parentheses.", "Eigenvector centrality, Number of Followers, and Days on Twitter in standard units and centered at zero"),
          notes.append=TRUE,font.size="small",label="tab: exp1Amain",float=TRUE,
          title="Study 1: Effects of Direct Message Treatments on Participation and Tweeting")
#sink()

#sink("exp1Bmain.tex")
stargazer(button.tweeted.1,buttonbytreat.tweeted.1,
          coef=makecoefslist(list(button.tweeted.1,buttonbytreat.tweeted.1)),
          se=makerobustseslist(list(button.tweeted.1,buttonbytreat.tweeted.1)),
          title="Study 1: Effects of Tweet Encouragement",
          style="apsr", column.sep.width = "0pt",  digits=3,
          omit.stat=c("adj.rsq", "f", "ser"),
          dep.var.labels=c("Tweeted"),
          covariate.labels=c("Shown Tweet Encouragement", "Treatment: Organizer", "Encouragement X Organizer", "Constant (Treatment: No Encouragment)"),
          align=TRUE,
          notes=c("Robust standard errors in parentheses."),
          notes.append=TRUE,font.size="small",label="tab: exp1Bmain",float=TRUE)
#sink()

#sink("exp1Bexposure.tex")
  stargazer(exposure.1, exposure.signed.1, exposure.signed.iv.1, exposure.tweeted.1, exposure.tweeted.iv.1,
            coef=makecoefslist(list(exposure.1, exposure.signed.1, exposure.signed.iv.1, exposure.tweeted.1, exposure.tweeted.iv.1)),
            se=makerobustseslist(list(exposure.1, exposure.signed.1, exposure.signed.iv.1, exposure.tweeted.1, exposure.tweeted.iv.1)),
            style="apsr", column.sep.width = "0pt",  digits=3,
            omit.stat=c("adj.rsq", "f", "ser"),
            column.labels=c("OLS", "OLS", "IV", "OLS", "IV"),
            model.names=FALSE,
            dep.var.labels=c("Shown Tweet", "Signed", "Tweeted"),
            covariate.labels=c("Exposure: Followed Subject Shown Tweet Encouragement","Exposure: Followed Subject Tweeted",  "Constant"),
            #align=TRUE,
            notes=c("Robust standard errors in parentheses.", "All regressions weighted by inverse probability of exposure."),
            notes.append=TRUE,font.size="small",label="tab: exp1Bexposure",float=TRUE,
            title="Study 1: Effects of Tweet Encouragement on Subjects' Followers")
#sink()

#sink("exp2Amain.tex")
stargazer(treat.signed.2, treat.signed.cov.2, treat.tweeted.2, treat.tweeted.cov.2,
          coef=makecoefslist(list(treat.signed.2, treat.signed.cov.2, treat.tweeted.2, treat.tweeted.cov.2)),
          se=makerobustseslist(list(treat.signed.2, treat.signed.cov.2, treat.tweeted.2, treat.tweeted.cov.2)),
          style="apsr", column.sep.width = "0pt",  digits=3, digits.extra=0,
          omit.stat=c("adj.rsq", "f", "ser"),
          dep.var.labels=c("Signed", "Tweeted"),
          covariate.labels=c("Treatment: Follower", "Treatment: Organizer", 
                             "Account Type: Male", "Account Type: Organization", "Account Type: Unknown",
                             "Eigenvector Centrality", "Number of Followers", "Days on Twitter", "Days on Twitter Missing",  "Constant"),
          align=TRUE,
          notes=c("Robust standard errors in parentheses.", "Eigenvector centrality, Number of Followers, and Days on Twitter in standard units and centered at zero"),
          notes.append=TRUE,font.size="small",label="tab: exp2Amain",float=TRUE,
          title="Study 2: Effects of Direct Message Treatments on Participation and Tweeting")
#sink()

#sink("exp2Bmain.tex")
stargazer(button.tweeted.2,buttonbytreat.tweeted.2,
          coef=makecoefslist(list(button.tweeted.2,buttonbytreat.tweeted.2)),
          se=makerobustseslist(list(button.tweeted.2,buttonbytreat.tweeted.2)),
          style="apsr", column.sep.width = "0pt",  digits=3,
          omit.stat=c("adj.rsq", "f", "ser"),
          dep.var.labels=c("Tweeted"),
          covariate.labels=c("Shown Tweet Encouragement", "Treatment: Organizer", "Encouragement X Organizer", "Constant (Treatment: No Encouragment)"),
          #align=TRUE,
          notes=c("Robust standard errors in parentheses."),
          notes.append=TRUE,font.size="small",label="tab: exp2Bmain",float=TRUE,
          title="Study 2: Effects of Tweet Button on Subsequent Tweets")
#sink()

#sink("exp2Bexposure.tex")
stargazer(exposure.2, exposure.signed.2, exposure.signed.iv.2, exposure.tweeted.2, exposure.tweeted.iv.2,
          coef=makecoefslist(list(exposure.2, exposure.signed.2, exposure.signed.iv.2, exposure.tweeted.2, exposure.tweeted.iv.2)),
          se=makerobustseslist(list(exposure.2, exposure.signed.2, exposure.signed.iv.2, exposure.tweeted.2, exposure.tweeted.iv.2)),
          style="apsr", column.sep.width = "0pt",  digits=3,
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels=c("OLS", "OLS", "IV", "OLS", "IV"),
          model.names=FALSE,
          dep.var.labels=c("Shown Tweet", "Signed", "Tweeted"),
          covariate.labels=c("Exposure: Followed Subject Shown Tweet Encouragement","Exposure: Followed Subject Tweeted",  "Constant"),
          #align=TRUE,
          notes=c("Robust standard errors in parentheses.", "All regressions weighted by inverse probability of exposure."),
          notes.append=TRUE,font.size="small",label="tab: exp2Bexposure",float=TRUE,
          title="Study 2: Effects of Tweet Encouragement on Subjects' Followers")
#sink()

## Heterogenous effects by account type

type= factor(x = c("Female", "Female", "Male", "Male", 
                   "Organization", "Organization", "Unknown", "Unknown"),
             levels = c("Unknown", "Organization", "Male", "Female"),
             ordered=TRUE)

prepareforgg <- function(DV, df){
  ggframe <- 
    group_by(df, account_type) %>%
    do(mod=lm(formula=paste0(DV," ~ treat"), data=.)) %>%
    do(data.frame(var = names(coef(.$mod)[2:3]),
                  coef(summary(.$mod))[2:3,1:2])) %>%
    `colnames<-`(c("Treatment", "CATE", "SE")) %>%
    ungroup() %>%
    mutate(DV=DV,type=type, 
           Treatment = ifelse(Treatment=="treatfol", "Follower", "Organizer"))
  return(ggframe)
}


data <- rbind(prepareforgg("signed", LCVexp1),
              prepareforgg("tweeted", LCVexp1),
              prepareforgg("signed", LCVexp2),
              prepareforgg("tweeted", LCVexp2))
data$graph_index <- rep(c("signed_1", "tweeted_1","signed_2", "tweeted_2"),each=8)
data$study <- rep(c("Study 1", "Study 2"),each=16)

g <- ggplot(data =data , aes(y=CATE, x=type, group=Treatment)) + 
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_pointrange(aes(y = CATE, 
                      ymin = CATE - SE*1.96,
                      ymax = CATE + SE*1.96,
                      shape=Treatment),
                  position=position_dodge(width=-.5))+
  scale_shape_manual(values=c(19,21))+
  ylim(-0.025, 0.10) + xlab("Account Type") + ylab("Conditional Average Treatment Effects") +
  ggtitle("Heterogeneous Effects of Treatment by Account Type") + 
  coord_flip() + theme_light() + theme(legend.position="bottom") + facet_grid(study ~ DV)

ggsave("Het_account_type.png", g,width = 7, height=7)


